package com.wang.JinDong2;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * 用户模块
 */
public class User implements LogIn, Checking {
    static Map<Integer, ProductInShop> userMap = new LinkedHashMap();
    static Map<Integer, ProductInShop> boughtMap = new LinkedHashMap<>();
    static int userMoney = 600000;
    static int count = 0;
    /**
     * 初始化账号密码
     */
    final String userName = "123";
    final String passWord = "123";
    static Scanner input = new Scanner(System.in);

    /**
     * 用户登录
     *
     * @return
     */
    @Override
    public boolean logIn() {
        Scanner input = new Scanner(System.in);
        System.out.print("请输入你的用户名：");
        String userName = input.nextLine();
        System.out.print("请输入你的密码：");
        String passWord = input.nextLine();
        return check(userName, passWord);
    }

    /**
     * 校验账号密码
     *
     * @param userName 用户名
     * @param passWord 密码
     * @return 登录状态
     */
    @Override
    public boolean check(String userName, String passWord) {
        if (this.userName.equals(userName) && this.passWord.equals(passWord)) {
            return true;
        }
        return false;
    }

    /**
     * 添加购物车
     */
    public void addShopCar() {
        Store.showAll();
        System.out.print("请选择添加产品的编号：");
        int buyId = input.nextInt();
        if (checkStoreExist(buyId)) {
            System.out.print("请选择添加此商品的数量:");
            int buyShopCarNum = input.nextInt();
            if (checkShopCarRepeat(buyId)) {
                userMap.get(buyId).setStore(userMap.get(buyId).getStore() + buyShopCarNum);
                System.out.println("添加成功");
                Pause.pause();
                return;
            }
            ProductInShop tempProduct = (ProductInShop) check(buyId);
            if (tempProduct.getStore() < buyShopCarNum) {
                System.out.println("数量不能大于库存，请重新输入");
                Pause.pause();
                return;
            }
            if (tempProduct != null) {
                tempProduct.setStore(buyShopCarNum);
                userMap.put(tempProduct.getId(), tempProduct);
                System.out.println("添加成功");
                Seller.waitBuyerPay++;
            }
        } else {
            System.out.println("未找到想要添加的产品的编号");
        }
        Pause.pause();
    }

    /**
     * 删除修改购物车
     */
    public void deleteShopCar() {
        checkShopCar();
        System.out.print("请输入您想删除的商品的编号：");
        int deleteShopId = input.nextInt();
        if (checkShopCarExist(deleteShopId)) {
            System.out.print("请输入您想删除此商品的数量：");
            int deleteShopNum = input.nextInt();
            changeNum(deleteShopId, deleteShopNum);
            System.out.println("删除成功");
            Pause.pause();
        } else {
            System.out.println("未找到删除编号的商品");
            Pause.pause();
        }
    }

    /**
     * 购买商品购物车
     */
    public void buyShopCar() {
        if (checkShopCar()) {
            System.out.println("-----***********-------");
            System.out.print("请输入您想购买的产品的编号：");
            int buyShopId = input.nextInt();
            if (checkShopCarExist(buyShopId)) {
                System.out.println("请输入您想购买的产品的数量");
                int buyShopNum = input.nextInt();
                ProductInShop pp = userMap.get(buyShopId);
                if (userMap.get(buyShopId).getStore() < buyShopNum) {
                    System.out.println("数量输入有错，请重新输入");
                    Pause.pause();
                    return;
                }
                int carPrice = userMap.get(buyShopId).getPrice() * buyShopNum;
                if (carPrice > userMoney) {
                    System.out.println("抱歉，余额不足，请充值！！");
                    Pause.pause();
                    return;
                }
                changeNum(buyShopId, buyShopNum);
                for (Product p : Store.list) {
                    ProductInShop temp = (ProductInShop) p;
                    if (temp.getId() == buyShopId) {
                        temp.setStore(temp.getStore() - buyShopNum);
                    }
                }
                count++;
                ProductInShop boughtList = new ProductInShop(pp.getId(), pp.getName(), pp.getPrice(),
                        pp.getColor(), pp.getSize(), buyShopNum);
                boughtMap.put(count, boughtList);

                System.out.println("购买成功，您本次消费:" + carPrice + "" +
                        "剩余余额为" + (userMoney - carPrice));
                userMoney -= carPrice;
                Seller.profit += carPrice;
                Seller.buyerPaid++;
            } else {
                System.out.println("未找到想要购买的对应的商品的编号");
                Pause.pause();
            }

        }
        Pause.pause();
    }

    /**
     * 检查购物车
     *
     * @return
     */
    public boolean checkShopCar() {
        System.out.println("-----↓当前购物车↓-----");
        Store.titleFrame();
        if (userMap.size() == 0) {
            System.out.println("当前购物车暂无任何产品，请去商城挑选吧");
            return false;
        }
        userMap.forEach((id, product) -> {
            product.show();
        });
        return true;
    }

    /**
     * 查看当前商品
     *
     * @param id
     * @return
     */
    public static Product check(int id) {
        if (Store.list.size() == 0) {
            System.out.println("当前无任何产品，请等待卖家添加产品");
            Pause.pause();
            return null;
        } else {
            for (Product p : Store.list) {
                if (p.getId() == id) {
                    ProductInShop p1 = (ProductInShop) p;
                    ProductInShop temp = new ProductInShop(p1.getId(), p1.getName(),
                            p1.getPrice(), p1.getColor(), p1.getSize(), p1.getStore());
                    return temp;
                }
            }
        }
        System.out.println("未找到此编号的产品");
        Pause.pause();
        return null;
    }

    /**
     * 修改
     *
     * @param changeId
     * @param changeNum
     */
    public static void changeNum(int changeId, int changeNum) {
        if (changeNum > userMap.get(changeId).getStore()) {
            System.out.println("输入数量有误！");
            return;
        }
        if (changeNum == userMap.get(changeId).getStore()) {
            userMap.remove(changeId);
        } else {
            userMap.get(changeId).setStore(userMap.get(changeId).getStore() - changeNum);
        }
    }

    public static void changeStoreNum(int changeId, int changeNum) {

    }

    public static boolean checkShopCarExist(int id) {
        if (userMap.size() == 0) {
            return false;
        } else {
            if (userMap.get(id) == null) {
                return false;
            } else {
                return true;
            }
        }

    }

    public static boolean checkStoreExist(int id) {
        if (Store.list.size() == 0) {
            return false;
        } else {
            for (Product p : Store.list) {
                if (p.getId() == id) {
                    return true;
                }
            }
            return false;
        }
    }

    public static void checkHistoryBuy() {
        if (boughtMap.size() == 0) {
            System.out.println("您现在暂无购买记录，快去购买吧！");
            return;
        }
        Store.titleFrame();
        boughtMap.forEach((count, product) -> product.show());
        Pause.pause();
    }

    public static boolean checkShopCarRepeat(int id) {
        if (userMap.get(id) == null) {
            return false;
        }
        return true;
    }

}
